home *** CD-ROM | disk | FTP | other *** search
Java Source | 1996-06-22 | 1.7 KB | 85 lines | [TEXT/Rstr] |
- import java.net.*;
- import java.io.*;
- import java.util.*;
-
- /*
- class WACGI
- {
- static native void SendRequest(String request);
- static native String GetResponse();
- static native void Done();
- }
- */
- public class TinyHTTPd
- {
- public static void main( String argv[] ) throws IOException
- {
- int port = 80;
-
- //System.out.println( "HTTPd starting..." );
-
- if (argv.length >= 1)
- port = Integer.parseInt(argv[0]);
- System.out.println( "HTTPd starting on port " + port + "...");
- ServerSocket ss = new ServerSocket(port);
-
- while (true)
- {
- Socket sock = ss.accept();
-
- try
- {
- OutputStream out = sock.getOutputStream();
- String req = new DataInputStream(sock.getInputStream()).readLine();
- System.out.println( "Request:" + req );
-
- StringTokenizer st = new StringTokenizer(req);
- String firstToken = st.nextToken();
-
- boolean another = true;
-
- if ((st.countTokens() >= 2) &&
- firstToken.equals("GET"))
- {
- if ( (req = st.nextToken()).startsWith("/"))
- req = req.substring(1);
-
- if (req.equals("QUIT"))
- another = false;
- else
- {
- req = "/Ricasso/cgi.txt";
- }
- try
- {
- FileInputStream fis = new FileInputStream(req);
- int av = fis.available();
- byte [] data = new byte [av];
- fis.read(data);
- out.write(data);
- out.flush();
- //out.close();
- }
- catch (FileNotFoundException e)
- new PrintStream(out).println("404 Not Found");
- }
- else
- new PrintStream(out).println("400 Bad Request");
-
- sock.close();
- if (!another)
- {
- System.out.println("Quitting...");
- //WACGI.Done();
- break;
- }
- }
- catch (IOException e)
- System.out.println("I/O Error " + e);
-
- }
-
- }
- }
-
-